package org.dllwh.milvus.util;

import com.google.common.collect.Lists;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import lombok.extern.slf4j.Slf4j;
import org.dllwh.milvus.model.AjaxResult;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName MilvusHelper
 * @Description Milvus帮助工具
 * @Author: <a href="mailto:duleilewuhen@sina.com">独泪了无痕</a>
 * @Date 2025-05-24 00:59
 * @版本 V 1.0.1
 * @since JDK 17
 */
@Slf4j
public final class MilvusHelper {
    /**
     * @param collectName 集合名称
     * @param operate     操作
     * @param response    响应结果
     */
    public static AjaxResult handleRpcStatus(String collectName, String operate, R<RpcStatus> response) {
        int status = response.getStatus();
        if (R.Status.Success.getCode() != status) {
            log.error("对集合【{}】的【{}】操作失败，失败原因是:{}", collectName, operate, response.getMessage());
            return AjaxResult.fail(status, response.getMessage());
        }
        Exception exception = response.getException();
        if (exception != null) {
            log.error("对集合【{}】的【{}】操作失败，失败原因是:{}", collectName, operate, exception.getMessage());
            return AjaxResult.fail(status, exception.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 构建向量数据
     */
    public static List<Float> buildRandomFloatArrayList(int size) {
        List<Float> floatList = Lists.newArrayList();
        if (size <= 0) {
            return floatList;
        }
        BigDecimal thousand = BigDecimal.valueOf(1000d);
        Random random = new Random();

        for (int i = 0; i < size; i++) {
            float v = thousand.multiply(BigDecimal.valueOf(random.nextDouble())).setScale(3, RoundingMode.HALF_UP).floatValue();
            floatList.add(v);
        }
        System.out.println(floatList);
        return floatList;
    }

}
